package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Shareholder;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.ShareholderService;
import com.csr.csrcheck.util.JsonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping({"shareholder"})
@RestController
public class ShareholderController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.ShareholderController.class);

    @Resource
    private ShareholderService shareholderService;

    @Resource
    private CompanyService companyService;

    @RequestMapping(path = {"addshareholder"}, method = {RequestMethod.POST})
    public JsonResult<Object> addshareholder(Shareholder shareholder) {
        this.shareholderService.addShareholder(shareholder);
        log.info("add================================>approvals"+ shareholder);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"listcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> listcompany() {
        List list = this.companyService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"updateshareholder"}, method = {RequestMethod.POST})
    public JsonResult<Void> updateshareholder(Shareholder shareholder) {
        this.shareholderService.updateShareholder(shareholder);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findShareholderByid/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Shareholder> findShareholderByid(@PathVariable("id") int id) {
        Shareholder list = null;
        try {
            list = this.shareholderService.findshareholderByid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"deleteshareholder/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deleteshareholder(@PathVariable("id") int id) {
        this.shareholderService.deleteshareholderByid(id);
        log.info(""+ id);
        return new JsonResult(Integer.valueOf(0), "json");
    }
}
